package com.dongzhu1428.express.module1.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dongzhu1428.express.TIP;
import com.dongzhu1428.express.module1.ENUM.TASK_RESULT;
import com.dongzhu1428.express.module1.Result;
import com.dongzhu1428.express.module1.dto.controllerDto.Message.SendMessageParamDto;
import com.dongzhu1428.express.module1.dto.controllerDto.Task.FinishTaskManuallyParamDto;
import com.dongzhu1428.express.module1.entity.DeliveryTask;
import com.dongzhu1428.express.module1.mapper.express.DeliveryTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.util.List;

@Service
public class TaskService {

    @Autowired
    DeliveryTaskMapper deliveryTaskMapper;

    @Autowired
    MessageService messageService;


    /**
     * 得到用户任务
     *
     * @param isDeliveryman 是否为送货人
     * @param keyId         键id
     * @param state         状态
     * @return {@link Result}<{@link List}<{@link DeliveryTask}>>
     */
    public Result<List<DeliveryTask>> getTaskOfUser(boolean isDeliveryman,int keyId,int state){
        String key = isDeliveryman ? "courier_id" : "user_id";
        List<DeliveryTask>  task = deliveryTaskMapper.selectList(new QueryWrapper<DeliveryTask>().eq(key,keyId).eq("state",state));
        return Result.success(task);

    }


    /**
     * 完成任务
     *
     * @param taskId 任务id
     * @return {@link Result}<{@link TIP}>
     */
    public  Result<TIP> finishedTask(int taskId){
        deliveryTaskMapper.update(null,new UpdateWrapper<DeliveryTask>().eq("id",taskId)
                        .set("complete_time",new Timestamp(System.currentTimeMillis()))
                .set("state",2));
        return Result.success(null,"任务状态已设置为完成");
    }

    /**
     * 取消任务
     *
     * @param taskId 任务id
     * @return {@link Result}<{@link TIP}>
     */
    public  Result<TIP> cancelTask(int taskId){
        deliveryTaskMapper.deleteById(taskId);
        return Result.success(null,"任务取消成功");
    }


    /**
     * 快递员手动完成任务
     *
     * @param dto dto
     * @return {@link Result}<{@link TIP}>
     */
    public  Result<TIP> finishTaskManually( FinishTaskManuallyParamDto dto){

        DeliveryTask deliveryTask = deliveryTaskMapper.selectById(dto.getTaskId());

        /**
         * 修改任务配送状态和结果状态
         */
        deliveryTaskMapper.update(null,new UpdateWrapper<DeliveryTask>().eq("id",dto.getTaskId()).set("state",2).set("result", TASK_RESULT.FINISH_PENDING.name()));
        /**
         * 以快递员身份给用户发信息
         */
        for (MultipartFile image : dto.getImages()) {
            messageService.sendMessage(SendMessageParamDto.builder()
                            .image(image)
                            .receiver(deliveryTask.getUserId())
                            .sender(deliveryTask.getCourierId())

                    .build());
        }

        if(dto.getMsg() !=null){
            messageService.sendMessage(SendMessageParamDto.builder()
                            .text(dto.getMsg())
                            .sender(deliveryTask.getCourierId())
                            .receiver(deliveryTask.getUserId())
                    .build());
        }

        /**
         * 以系统的身份给用户发信息
         */
        messageService.sendMessage(SendMessageParamDto.builder()
                .text("您的快递任务"+deliveryTask.getId()+"已送达，请前往已完成任务中进行确认，如您两天内不进行未送达操作，系统将自动判定快递员完成任务，将赏金给到快递员")
                .sender(0)
                .receiver(deliveryTask.getUserId())
                .build());
        return  Result.success(TIP.成功);

    }

    public  Result<TIP>  undelivered( int taskId){
      deliveryTaskMapper.update(null,new UpdateWrapper<DeliveryTask>()
              .eq("id",taskId)
              .set("result",TASK_RESULT.ARRIVE_PENDING.name())
      );

      return  Result.success(TIP.成功);
    }

}
